package datamodel;

import communication.Controller;
import communication.Parameter;
import communication.RestFulService;
import communication.ServiceCommunicator;

public class ServiceCommunicatorHelper {
	public static String getParameterList(ServiceCommunicator communicator) {
		RestFulService service = communicator.getUseServices();
		String parameterList = "";
		for (int index = 0; index < service.getParameters().size(); index++) {
			Parameter parameter = service.getParameters().get(index);
			parameterList += parameter.getParameterType().getName() + " "
					+ parameter.getName();
			if (index != service.getParameters().size() - 1) {
				parameterList += ", ";
			}
		}
		return parameterList;
	}

	public static String getDomainURL(ServiceCommunicator communicator) {
		RestFulService service = communicator.getUseServices();
		Controller controller = (Controller) service.eContainer();
		String controllerURL = controller.getUrlMapping() == null ? ""
				: controller.getUrlMapping();
		String serviceURL = service.getUrlMapping() == null ? "" : service
				.getUrlMapping();
		return controllerURL + serviceURL;
	}

	public static String getReturnType(ServiceCommunicator communicator) {
		RestFulService service = communicator.getUseServices();
		if (service.getResponse() == null)
			return "void";
		else {
			String responeType = communicator.getUseServices().getResponse()
					.getName();

			if (responeType.endsWith("String"))
				return "AcknowledgePacket";
			else
				return responeType;
		}
	}

	public static Boolean hasReturnType(ServiceCommunicator communicator) {
		RestFulService service = communicator.getUseServices();
		if (service.getResponse() == null)
			return false;
		else
			return true;
	}

	public static String getHttpMethodVariable(ServiceCommunicator communicator) {
		RestFulService service = communicator.getUseServices();
		if (service.getHttpMethod().toLowerCase().equals("post"))
			return "httppost";
		else if (service.getHttpMethod().toLowerCase().equals("get"))
			return "httpget";
		return "";
	}
}
